from tkinter import *

import tkinter
from PIL import Image, ImageTk, ImageSequence
import time
#
# root = Tk()
# root.geometry("300x300")
# canvas = Canvas(root,width=300, height=300,bg='white')
# canvas.pack()
#
#
# #分解gif并逐帧显示
# def pick(event):
#     global a,flag
#     while 1:
#         im = Image.open('image/pikaqiu.gif')
#         # GIF图片流的迭代器
#         iter = ImageSequence.Iterator(im)
#         #frame就是gif的每一帧，转换一下格式就能显示了
#         for frame in iter:
#             pic=ImageTk.PhotoImage(frame)
#             canvas.create_image(100,150,anchor = 'nw', image=pic)
#             time.sleep(0.1)
#             root.update_idletasks()  #刷新
#             root.update()
#
# canvas.bind("<Enter>",pick)  #这个事件是鼠标进入组件，用什么事件不重要，这里只是演示
# root.mainloop()
# from tkinter import *
# import time
# import os
# root = Tk()
#
# numIdx = 5 # gif的帧数
# # 填充6帧内容到frames
# frames = [PhotoImage(file='image/pikaqiu.gif', format='gif -index %i' %(i)) for i in range(numIdx)]
#
# def update(idx): # 定时器函数
#     frame = frames[idx]
#     idx += 1 # 下一帧的序号：在0,1,2,3,4,5之间循环(共6帧)
#     label.configure(image=frame) # 显示当前帧的图片
#     root.after(100, update, idx%numIdx) # 0.1秒(100毫秒)之后继续执行定时器函数(update)
#
# label = Label(root)
# label.pack()
# root.after(0, update,0) # 立即启动定时器函数(update)
# root.mainloop()
#
#
#
# import PIL.Image as Image
# #
# #制作GIF
# def get_gif(pic_dir , n , t=0.1) :
#     imgs = []
#     for i in range(n) :
#         pic_name = '{}/pho ({}).png'.format(pic_dir , i)
#         temp = Image.open(pic_name)
#         imgs.append(temp)
#     save_name = 'pho/{}.gif'.format(pic_dir)
#     imgs[0].save(save_name , save_all=True , append_images=imgs , duration=t)
#     return save_name
#
#
# if __name__ == '__main__' :
#     pic_dir = 'pho'
#     save_name = get_gif(pic_dir , 18 , 2000)
#     print('制作完成。所属文件:{}'.format(save_name))
#
#
#
#
#倒放GIF
# im = Image.open('gif/boss.gif')
# img = []
#
# for frame in ImageSequence.Iterator(im) :
#     img.append(frame.copy())
# img.reverse()
# img[0].save(r'image/pipi.gif',save_all=True,append_images=img[1:])

# #分解GIF
im = Image.open('gif/big.gif')
img = []
i=0
for frame in ImageSequence.Iterator(im) :
    img.append(frame.copy())
    i+=1
    frame.save(r'gif/out_'+str(i)+'.png')
#img.reverse()